﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Emmajn.ChildParentExtension.SilverlightClassLibrary
{
    /// <summary>
    /// Represents an address.
    /// </summary>
    public class Address
    {
        public const int MaxFirstLineLength = 25;
        public const int MaxSecondLineLength = 25;
        public const int MaxCityLength = 14;
        public const int MaxAreaLength = 14;
        public const int MaxPostalCodeLength = 10;
        public const int MaxCountryLength = 20;

        private string firstLine;
        private string secondLine;
        private string city;
        private string area;
        private string postalCode;
        private string country;

        /// <summary>
        /// Initializes a new instance of the Address class.
        /// </summary>
        /// <param name="firstLine">First line of address (up to 25 characters)</param>
        /// <param name="secondLine">[Second line of address (up to 25 characters)]</param>
        /// <param name="city">City (up to 14 characters)</param>
        /// <param name="area">State, Province, or Region (up to 14 characters)</param>
        /// <param name="country">Country (up to 20 characters)</param>
        /// <param name="postalCode">[Postal Code (up to 10 letters, digits, hyphens, and blanks)]</param>
        public Address(string firstLine, string secondLine, string city, string area, string country, string postalCode = null)
        {
            FirstLine = firstLine;
            SecondLine = secondLine;
            City = city;
            Area = area;
            Country = country;
            PostalCode = postalCode;
        }

        /// <summary>
        /// First line of address (up to 25 characters)
        /// </summary>
        public string FirstLine
        {
            get
            {
                return firstLine;
            }
            set
            {
                Assert.IsValidRequiredString(value, MaxFirstLineLength);
                firstLine = value;
            }
        }

        /// <summary>
        /// [Second line of address (up to 25 characters)]
        /// </summary>
        public string SecondLine
        {
            get
            {
                return secondLine;
            }
            set
            {
                Assert.IsValidRequiredString(value, MaxSecondLineLength);
                secondLine = value;
            }
        }

        /// <summary>
        /// City (up to 14 characters)
        /// </summary>
        public string City
        {
            get
            {
                return city;
            }
            set
            {
                Assert.IsValidRequiredString(value, MaxCityLength);
                city = value;
            }
        }

        /// <summary>
        /// State, Province, or Region (up to 14 characters)
        /// </summary>
        public string Area
        {
            get
            {
                return area;
            }
            set
            {
                Assert.IsValidRequiredString(value, MaxAreaLength);
                area = value;
            }
        }

        /// <summary>
        /// [Postal Code (up to 10 letters, digits, hyphens, and blanks)]
        /// </summary>
        public string PostalCode
        {
            get
            {
                return postalCode;
            }
            set
            {
                Assert.IsValidRequiredString(value, MaxPostalCodeLength);
                postalCode = value;
            }
        }

        /// <summary>
        /// Country (up to 20 characters)
        /// </summary>
        public string Country
        {
            get
            {
                return country;
            }
            set
            {
                Assert.IsValidRequiredString(value, MaxCountryLength);
                country = value;
            }
        }

        /// <summary>
        /// Returns a string that represents the current object.
        /// </summary>
        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine(firstLine);
            if (secondLine != null) stringBuilder.AppendLine(secondLine);
            stringBuilder.AppendFormat("{0}, {1}", city, area);
            if (postalCode != null) stringBuilder.AppendFormat(", {0}", postalCode);
            stringBuilder.AppendLine();
            stringBuilder.Append(country);
            return stringBuilder.ToString();
        }
    }
}
